using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace TaskMaster
{
    public partial class MainForm : Form
    {
        //List<Task> taskList = new List<Task>();
        //List<TimeRecord> timeRecords = new List<TimeRecord>();
        Activity activeTask;
        TaskParametersUI newTaskForm;
        ActivityRecord activeTimeRecord;
        Activity task_createNewTask;
        Activity task_freeTime;
        Activity task_modifyTask;


        public MainForm()
        {
            InitializeComponent();

            // These are special system tasks and should not be modifiable by the user.
            // Labeling them with the 'system' tag will prevent user modification.
            //task_createNewTask.Tags.Add("system");
            //task_freeTime.Tags.Add("system");
            //task_modifyTask.Tags.Add("system");
        }

        //private void newTask_cancel_button_Click(object sender, EventArgs e)
        //{
        //    newTaskForm.Close();
        //}

        private void comboBox_Tasks_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Record time against tasks - JMS

            //Record stop time for previous task
            activeTimeRecord.EndTime = DateTime.Now;
            //Create new active time record for current task
            activeTask = (Activity)comboBox_Tasks.SelectedItem;
            activeTimeRecord = activeTask.NewRecord();
            //Record start time for new active task
            activeTimeRecord.StartTime = DateTime.Now;

            if (activeTask.SelectTask != null) { activeTask.SelectTask(); }

            //if (((Task)comboBox_Tasks.SelectedItem).TaskID == task_createNewTask.TaskID)
            //{
            //    newTaskForm = new form_TaskCreation();
            //    newTaskForm.Controls["button_ok"].Click += new EventHandler(this.newTask_ok_button_Click);
            //    newTaskForm.Controls["button_cancel"].Click += new EventHandler(this.newTask_cancel_button_Click);
            //    newTaskForm.ShowDialog();
            //}
        }

        //private void newTask_ok_button_Click(object sender, EventArgs e)
        //{
        //    if (newTaskForm.NewTask != null)
        //    {
        //        taskList.Add(newTaskForm.NewTask);
        //        //Manually force comboBox_Tasks to reread the datasource.
        //        BindingManagerBase bindMB = comboBox_Tasks.BindingContext[taskList];
        //        // Unsuscribe from the SelectedIndexChanged event as we do not want to handle this event for the next few statements.
        //        comboBox_Tasks.SelectedIndexChanged -= comboBox_Tasks_SelectedIndexChanged;
        //        bindMB.SuspendBinding();           
        //        bindMB.ResumeBinding();
        //        // Change selected task back to 'Create New Task' after the binding reset.
        //        comboBox_Tasks.SelectedItem = task_createNewTask;
        //        comboBox_Tasks.SelectedIndexChanged += comboBox_Tasks_SelectedIndexChanged;
        //    }
        //    newTaskForm.Close();
        //}

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Initialize Task Toolbar - JS

            // Create initial time record for default task
            //Must create active time record before adding default tasks to combobox_Tasks.
            //If this is not done, activeTimeRecord will be null with the SelectedIndexChanged event for the combobox is fired when the datasource is specified. - JMS

            using (TaskMasterEntities1 entities = new TaskMasterEntities1())
            {
                task_createNewTask = (from x in entities.Activity
                                      where x.ActivityName == "Create New Task"
                                      select x).Single();
                    //entities.Activity.Where(x => x.ActivityName == "Create New Task").Select(x => x).Single();

            }
            // Add default tasks
            activeTimeRecord = task_freeTime.NewRecord(); 
            //taskList.Add(task_freeTime);
            task_createNewTask.SelectTask += createNewTask_Selected;
            //taskList.Add(task_createNewTask);
            task_modifyTask.SelectTask += modifyTask_Selected;
            //taskList.Add(task_modifyTask);

            //comboBox_Tasks.DisplayMember = "TaskName";
            //comboBox_Tasks.DataSource = taskList;
            comboBox_Tasks.SelectedIndexChanged += new System.EventHandler(this.comboBox_Tasks_SelectedIndexChanged);
            activeTask = (Task)comboBox_Tasks.SelectedItem;
            // Record start time for initial time record
            activeTimeRecord.StartTime = DateTime.Now;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Calculate and display time elapsed.
            TimeSpan elapsedTime = DateTime.Now - activeTimeRecord.StartTime;
            string s = elapsedTime.ToString();
            label_elapsedTime.Text = s.Substring(0, s.IndexOf('.'));

            // Calculate and display total time.
            s = activeTask.ActualTime.ToString();
            //label_totalTime.Text = s.Substring(0, s.IndexOf('.'));
            label_totalTime.Text = s;

            // Calculate and display time remaining.
            TimeSpan timeLeft = activeTask.EstimatedTime - activeTask.ActualTime;
            s = timeLeft.ToString();
            //label_timeLeft.Text = s.Substring(0, s.IndexOf('.'));
            label_timeLeft.Text = s;
        }

        private void createNewTask_Selected()
        {
            newTaskForm = new TaskParametersUI();
            newTaskForm.Text = "New Task";
            //newTaskForm.Controls["button_ok"].Click += new EventHandler(this.newTask_ok_button_Click);
            //newTaskForm.Controls["button_cancel"].Click += new EventHandler(this.newTask_cancel_button_Click);
            DialogResult result = newTaskForm.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (newTaskForm.Task != null)
                {
                    taskList.Add(newTaskForm.Task);
                    Update_comboBox_Tasks(task_createNewTask);
                }
            }
        }

        private void modifyTask_Selected()
        {
            TaskSelectionUI taskSelector = new TaskSelectionUI(taskList);
            taskSelector.ShowDialog();

            if (taskSelector.DialogResult == DialogResult.OK)
            {
                TaskParametersUI modifyTaskForm = new TaskParametersUI(taskSelector.SelectedTask);
                modifyTaskForm.Text = "Modify Task";
                modifyTaskForm.ShowDialog();
                Update_comboBox_Tasks(task_modifyTask);
            }
        }

        private void Update_comboBox_Tasks(Task selectedItem)
        {
            //Manually force comboBox_Tasks to reread the datasource.
            BindingManagerBase bindMB = comboBox_Tasks.BindingContext[taskList];
            // Unsuscribe from the SelectedIndexChanged event as we do not want to handle this event for the next few statements.
            comboBox_Tasks.SelectedIndexChanged -= comboBox_Tasks_SelectedIndexChanged;
            bindMB.SuspendBinding();
            bindMB.ResumeBinding();
            // Change selected task back after the binding reset.
            comboBox_Tasks.SelectedItem = selectedItem;
            comboBox_Tasks.SelectedIndexChanged += comboBox_Tasks_SelectedIndexChanged;
        }
    }
}